//
//  StatusViewModel.swift
//  glzbowei
//
//  Created by 高伦展 on 2018/1/7.
//  Copyright © 2018年 glz. All rights reserved.
//

import UIKit

//微博视图模型 - 处理单条微博的业务逻辑
class StatusViewModel : CustomStringConvertible {
    
    //微博的模型
    var status: Status
    
    //表格可重用表示符号
    var cellId : String{
        return status.retweeted_status != nil ? StatusCellRetweetedId : StatusCellNormalId
    }
    
    //缓存行高
    lazy var rowHeight:CGFloat = {
        var cell : StatusCell
        
        if self.status.retweeted_status != nil{
            cell = StatusRetweetedCell(style: .default, reuseIdentifier: StatusCellRetweetedId)
        }else{
            cell = StatusNormalCell(style: .default, reuseIdentifier: StatusCellNormalId)
        }
        
        return cell.rowHeight(vm: self)
    }()
    
    //微博发布日期
    var createAt:String{
        return (Date.sinaDate(str: status.created_at ?? "")?.dateDescription)!
    }
    
    //用户头像 url
    var userProfileUrl:URL {
        return URL(string: status.user?.profile_image_url ?? "")!
    }
    
    //用户默认头像
    var userDefaultIconView:UIImage{
        return UIImage(named: "avatar_default_big")!
    }
    
    var userMemberImage: UIImage? {
        if (status.user?.mbrank)! > (0) && (status.user?.mbrank)! < (7){
            return UIImage(named:"common_icon_membership_level\(String(describing: status.user?.mbrank))")
        }
        return nil
    }
    
    var userVipImage:UIImage?{
        
        switch (status.user?.verified_type ?? -1) {
        case 0:
            return UIImage(named: "avatar_vip")
        case 2, 3, 5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return nil
        }
    }
    
    //缩略图URL数组 - 存储型属性
    var thumbnailUrls:[URL]?
    
    var retweetedText:String?{
        
        guard let s = status.retweeted_status else {
            return nil
        }
        
        return "" //"@" + (s.user?.screen_name ?? "") + ":" + (s.text ?? "")
    }
    
    //构造函数
    init(status:Status) {
        self.status = status
        
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
            thumbnailUrls = [URL]()
            
            for dict in urls {
                let url = URL(string: dict["thumbnail_pic"]!)
                
                thumbnailUrls?.append(url!)
            }
        }
    }
    
    //描述信息
    var description: String{
        
        return status.description + "配图数组  \(thumbnailUrls ?? [] as Array)"
    }
    
}
